package com.ihr360.message.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 消息发送记录
 *
 * @author tom
 * @date 17-12-28
 */
public class MessageSendRecord implements Serializable {

    public static class SendStatus {
        public final static int DEFAULT = 0;
        public final static int SUB_SUCCESS = 1;
        public final static int SUB_FAILED = -1;
        public final static int SEND_SUCCESS = 2;
        public final static int SEND_FAILED = -2;
    }

    private String taskId;

    /**
     * 通讯Id
     * 微信openId、短信mobile
     */
    private String sendId;

    private String placeholderValues;

    /**
     * 消息内容
     */
    private String content;

    /**
     * 发送状态
     */
    private int sendStatus;

    /**
     * 失败原因
     */
    private String failedReason;

    private String vendorTemplateId;

    /**
     * 记录对应的行id
     */
    private String spreaadsheetRowId;

    /**
     * 标题
     */
    private String title;


    public static MessageSendRecord generateOneClickSms(String sendId, String messageTemplate, String vendorTemplateId,
                                                        String companyName, String url, String month, String spreadsheetRowId) {
        MessageSendRecord messageSendRecord = new MessageSendRecord();
        messageSendRecord.setSendId(sendId);
        messageSendRecord.setSendStatus(SendStatus.DEFAULT);
        String content = messageTemplate;
        JSONArray dataList = new JSONArray();
        if (content.contains("{{company.DATA}}")) {
            JSONObject object = new JSONObject();
            object.put("name", "company");
            object.put("value", companyName);
            dataList.add(object);
            content = content.replaceAll("\\{\\{company\\.DATA\\}\\}", companyName);
        }
        if (content.contains("{{url.DATA}}")) {
            JSONObject object = new JSONObject();
            object.put("name", "url");
            object.put("value", url);
            dataList.add(object);
            content = content.replaceAll("\\{\\{url\\.DATA\\}\\}", url);
        }
        if (content.contains("{{month.DATA}}")) {
            JSONObject object = new JSONObject();
            object.put("name", "month");
            object.put("value", month);
            dataList.add(object);
            content = content.replaceAll("\\{\\{month\\.DATA\\}\\}", month);
        }
        messageSendRecord.setPlaceholderValues(dataList.toJSONString());
        messageSendRecord.setVendorTemplateId(vendorTemplateId);
        messageSendRecord.setContent(content);
        messageSendRecord.setSpreaadsheetRowId(spreadsheetRowId);
        return messageSendRecord;
    }

    public static MessageSendRecord generateOneClickEmailMessage(String sendId, String messageTemplate, String vendorTemplateId,
                                                                 String companyName, String userName, String month, String spreadsheetRowId, String imgQRcodeBase64, String url) {
        MessageSendRecord messageSendRecord = new MessageSendRecord();
        messageSendRecord.setSendId(sendId);
        messageSendRecord.setSendStatus(SendStatus.DEFAULT);
        String content = messageTemplate;
        if (content.contains("{{keyword1.DATA}}")) {
            content = content.replaceAll("\\{\\{keyword1\\.DATA\\}\\}", companyName);
        }
        if (content.contains("{{keyword2.DATA}}")) {
            content = content.replaceAll("\\{\\{keyword2\\.DATA\\}\\}", month);
        }
        if (content.contains("{{keyword3.DATA}}")) {
            content = content.replaceAll("\\{\\{keyword3\\.DATA\\}\\}", userName);
        }
        if (content.contains("{{keyword4.DATA}}")) {
            content = content.replaceAll("\\{\\{keyword4\\.DATA\\}\\}", url);
        }
        if (content.contains("{{keyword6.DATA}}")) {
            content = content.replaceAll("\\{\\{keyword6\\.DATA\\}\\}", imgQRcodeBase64);
        }

        messageSendRecord.setContent(content);
        messageSendRecord.setVendorTemplateId(vendorTemplateId);
        messageSendRecord.setSpreaadsheetRowId(spreadsheetRowId);
        messageSendRecord.setTitle(companyName + "给您发送了" + month + "的薪资");
        return messageSendRecord;
    }

    public static MessageSendRecord generateOneClickProgramMessage(String sendId, String messageTemplate, String vendorTemplateId,
                                                                   String companyName, String messageDesc, String month, String spreadsheetRowId) {
        MessageSendRecord messageSendRecord = new MessageSendRecord();
        messageSendRecord.setSendId(sendId);
        messageSendRecord.setSendStatus(SendStatus.DEFAULT);
        String content = messageTemplate;
        JSONArray dataList = new JSONArray();
        if (content.contains("{{keyword1.DATA}}")) {
            JSONObject object = new JSONObject();
            object.put("name", "keyword1");
            object.put("value", companyName);
            dataList.add(object);
            content = content.replaceAll("\\{\\{keyword1\\.DATA\\}\\}", companyName);
        }
        if (content.contains("{{keyword2.DATA}}")) {
            JSONObject object = new JSONObject();
            object.put("name", "keyword2");
            object.put("value", month);
            dataList.add(object);
            content = content.replaceAll("\\{\\{keyword2\\.DATA\\}\\}", month);
        }
        if (content.contains("{{keyword3.DATA}}")) {
            JSONObject object = new JSONObject();
            object.put("name", "keyword3");
            object.put("value", messageDesc);
            dataList.add(object);
            content = content.replaceAll("\\{\\{keyword3\\.DATA\\}\\}", messageDesc);
        }
        if (content.contains("{{keyword4.DATA}}")) {
            JSONObject object = new JSONObject();
            object.put("name", "keyword4");
            object.put("value", messageDesc);
            dataList.add(object);
            content = content.replaceAll("\\{\\{keyword4\\.DATA\\}\\}", messageDesc);
        }
        if (content.contains("{{keyword5.DATA}}")) {
            JSONObject object = new JSONObject();
            object.put("name", "keyword5");
            object.put("value", messageDesc);
            dataList.add(object);
            content = content.replaceAll("\\{\\{keyword5\\.DATA\\}\\}", messageDesc);
        }

        messageSendRecord.setPlaceholderValues(dataList.toJSONString());
        messageSendRecord.setVendorTemplateId(vendorTemplateId);
        messageSendRecord.setContent(content);
        messageSendRecord.setSpreaadsheetRowId(spreadsheetRowId);
        return messageSendRecord;
    }


    public static MessageSendRecord generateOneClickWechatMessage(String sendId, String messageTemplate, String vendorTemplateId,
                                                                  String companyName, String messageDesc, String month, String spreadsheetRowId) {
        MessageSendRecord messageSendRecord = new MessageSendRecord();
        messageSendRecord.setSendId(sendId);
        messageSendRecord.setSendStatus(SendStatus.DEFAULT);
        String content = messageTemplate;
        JSONArray dataList = new JSONArray();
        if (content.contains("{{first.DATA}}")) {
            String value = "信息提醒";
            JSONObject object = new JSONObject();
            object.put("name", "first");
            object.put("value", value);
            dataList.add(object);
            content = content.replaceAll("\\{\\{first\\.DATA\\}\\}", value);
        }
        if (content.contains("{{keyword1.DATA}}")) {
            JSONObject object = new JSONObject();
            object.put("name", "keyword1");
            object.put("value", companyName);
            dataList.add(object);
            content = content.replaceAll("\\{\\{keyword1\\.DATA\\}\\}", companyName);
        }
        if (content.contains("{{keyword2.DATA}}")) {
            JSONObject object = new JSONObject();
            object.put("name", "keyword2");
            object.put("value", month);
            dataList.add(object);
            content = content.replaceAll("\\{\\{keyword2\\.DATA\\}\\}", month);
        }
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String date = dtf.format(LocalDateTime.now());
        if (content.contains("{{keyword3.DATA}}")) {
            JSONObject object = new JSONObject();
            object.put("name", "keyword3");
            object.put("value", date);
            dataList.add(object);
            content = content.replaceAll("\\{\\{keyword3\\.DATA\\}\\}", date);
        }
        if (content.contains("{{remark.DATA}}")) {
            String value = "更多问题，请咨询单位HR，或致电400-806-2822";
            JSONObject object = new JSONObject();
            object.put("name", "remark");
            object.put("value", value);
            dataList.add(object);
            content = content.replaceAll("\\{\\{remark\\.DATA\\}\\}", value);
        }
        messageSendRecord.setPlaceholderValues(dataList.toJSONString());
        messageSendRecord.setVendorTemplateId(vendorTemplateId);
        messageSendRecord.setContent(content);
        messageSendRecord.setSpreaadsheetRowId(spreadsheetRowId);
        return messageSendRecord;
    }


    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public String getSendId() {
        return sendId;
    }

    public void setSendId(String sendId) {
        this.sendId = sendId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getSendStatus() {
        return sendStatus;
    }

    public void setSendStatus(int sendStatus) {
        this.sendStatus = sendStatus;
    }

    public String getFailedReason() {
        return failedReason;
    }

    public void setFailedReason(String failedReason) {
        this.failedReason = failedReason;
    }

    public String getPlaceholderValues() {
        return placeholderValues;
    }

    public void setPlaceholderValues(String placeholderValues) {
        this.placeholderValues = placeholderValues;
    }

    public String getVendorTemplateId() {
        return vendorTemplateId;
    }

    public void setVendorTemplateId(String vendorTemplateId) {
        this.vendorTemplateId = vendorTemplateId;
    }

    public String getSpreaadsheetRowId() {
        return spreaadsheetRowId;
    }

    public void setSpreaadsheetRowId(String spreaadsheetRowId) {
        this.spreaadsheetRowId = spreaadsheetRowId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public static void main(String[] args) {
        String str = "{{company.DATA}}";
        System.out.println(str.replaceAll("\\{\\{company\\.DATA\\}\\}", "11"));
    }


}
